﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CCWin;
using SuperMap.Data;

namespace MDF
{
    /// <summary>
    /// 设置空间阈值
    /// </summary>
    public partial class SJPPYZ : CCSkinMain
    {
        private Datasources m_datasources;
        //xml读写类
        private IO_xml m_Xml;
        //配置文件信息
        private string m_xmlName = "PiPeiYuZhi";
        private string m_infoName = "KongJianYuZhi";
        private string m_andStr = "..";
        private string[] m_keys;

        private Label m_label;
        private ComboBox m_comboBox;

        /// <summary>
        /// 匹配阈值初始化
        /// </summary>
        public SJPPYZ(Datasources datasources)
        {
            InitializeComponent();
            //获取数据源
            m_datasources = datasources;
            //实例化xml读写对象，保存相关参数信息
            m_Xml = new IO_xml(m_xmlName);
            m_keys = new string[] { "DistanceThershold", "LengthThershold" };
            
        }
        /// <summary>
        /// 匹配阈值初始化（检查用）
        /// </summary>
        public SJPPYZ()
        {
            InitializeComponent();
            //实例化xml读写对象，保存相关参数信息
            m_Xml = new IO_xml(m_xmlName);
            m_keys = new string[] { "DistanceThershold", "LengthThershold" };

        }
        /// <summary>
        /// 将窗体转为拓扑处理的窗体
        /// </summary>
        public void ChangeForm()
        {
            try
            {
                //修改配置文件信息,暂时以...作为拓扑处理的连接字符
                m_infoName = "TuoPuYuZhi";
                m_andStr = "...";
                m_keys = new string[] { "ErrorThershold", "ToleranceThershold", "DepthThershold" };
                //修改窗体样式
                this.Text = "拓扑处理";
                this.Size = new Size(238, 277);
                this.Distance_Label.Text = "保留阈值：";
                this.Length_Lable.Text = "节点容限：";
                this.Link_Button.Visible = true;
                this.Unit_Label.Dispose();
                m_label = new Label()
                {
                    Location = new Point(7, 86),
                    Name = "Error_Label",
                    Size = new Size(77, 14),
                    Text = "搜索深度："
                };
                m_comboBox = new ComboBox()
                {
                    DropDownStyle = ComboBoxStyle.DropDownList,
                    Font = new Font("楷体", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                    FormattingEnabled = true,
                    Location = new Point(77, 80),
                    Name = "Depth_ComboBox",
                    Size = new System.Drawing.Size(120, 20),
                    TabStop = false,
                };
                m_comboBox.Items.AddRange(new object[] { "1", "2", "3", "4" });
                m_comboBox.SelectedIndex = 0;
                this.Settings_GroupBox.Size = new Size(224, 113);
                this.Settings_GroupBox.Controls.Add(m_comboBox);
                this.Settings_GroupBox.Controls.Add(m_label);
                //调整位置
                this.Link_Button.Location = new Point(33, 246);
                this.Save_Button.Location = new Point(101, 246);
                this.No_Button.Location = new Point(168, 246);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 填充数据集选择列表
        /// </summary>
        public void FillSets()
        {
            try
            {
                //遍历数据源获取数据集名称
                foreach (Datasource datasource in m_datasources)
                {
                    foreach (Dataset dataset in datasource.Datasets)
                    {
                        ////判断是那个窗口填充
                        //if (this.Name == "KJPPYZ")
                        //{
                        //    this.Set1_Combo.Items.Add(dataset.Name);
                        //    this.Set2_Combo.Items.Add(dataset.Name);
                        //}
                        //else
                        //{
                        //    //拓扑处理只填充线数据集
                        //    if(dataset.Type == DatasetType.Line)
                        //    {
                        //        this.Set1_Combo.Items.Add(dataset.Name);
                        //        this.Set2_Combo.Items.Add(dataset.Name);
                        //    }
                        //}
                        this.Set1_Combo.Items.Add(dataset.Name);
                        this.Set2_Combo.Items.Add(dataset.Name);
                    }
                }
                //判断初始填充状态
                if(this.Set1_Combo.Items.Count > 0)
                {
                    this.Set1_Combo.SelectedIndex = 0;
                    this.Set2_Combo.Items.RemoveAt(0);
                    if (this.Set2_Combo.Items.Count > 0)
                    {
                        this.Set2_Combo.SelectedIndex = 0;
                        //查询是否存在阈值信息,并填充
                        GetInfo(this.Set1_Combo.Text, this.Set2_Combo.Text);
                    }
                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// set1选择项更新时，更新下拉列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set1_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //获取Set2下拉框选则的值
                string text = this.Set2_Combo.Text;
                //清空Set2下拉框
                this.Set2_Combo.Items.Clear();
                //重新填充Set2下拉框
                foreach (Datasource datasource in m_datasources)
                {
                    foreach (Dataset dataset in datasource.Datasets)
                    {
                        //判断是那个窗口填充
                        if (this.Name == "KJPPYZ")
                        {
                            this.Set2_Combo.Items.Add(dataset.Name);
                        }
                        else
                        {
                            ////拓扑处理只填充线数据集
                            //if (dataset.Type == DatasetType.Line)
                            //{
                            //    this.Set2_Combo.Items.Add(dataset.Name);
                            //}
                            this.Set2_Combo.Items.Add(dataset.Name);
                        }
                    }
                }
                //移除set1已选择项
                this.Set2_Combo.Items.Remove(this.Set1_Combo.Text);
                //是否相同
                if (this.Set1_Combo.Text == text)
                {
                    //相同不填充，恢复默认
                    if (this.Set2_Combo.Items.Count > 0)
                    {
                        this.Set2_Combo.SelectedIndex = 0;
                    }
                }
                else
                {
                    //不相同，恢复原选项
                    this.Set2_Combo.SelectedItem = text;

                }
                //查询阈值信息并填充
                GetInfo(this.Set1_Combo.Text, this.Set2_Combo.Text);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// set2选择时，更新填充信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set2_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //查询阈值信息并填充
                GetInfo(this.Set1_Combo.Text, this.Set2_Combo.Text);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 禁止更改数据集选则项
        /// </summary>
        /// <param name="set1name">数据集1的名称</param>
        /// <param name="set2name">数据集2的名称</param>
        public void Ban_ChangeSelections(string set1name, string set2name)
        {
            try
            {
                //填充选择项
                this.Set1_Combo.Items.Add(set1name);
                this.Set2_Combo.Items.Add(set2name);
                //取消选择控制
                this.Set1_Combo.SelectionChangeCommitted -= new System.EventHandler(Set1_Combo_SelectionChangeCommitted);
                this.Set2_Combo.SelectionChangeCommitted -= new System.EventHandler(Set2_Combo_SelectionChangeCommitted);
                //默认选择
                this.Set1_Combo.SelectedIndex = 0;
                this.Set2_Combo.SelectedIndex = 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取阈值信息并填充
        /// </summary>
        /// <param name="set1name">数据集1的名称</param>
        /// <param name="set2name">数据集2的名称</param>
        private void GetInfo(string set1name, string set2name)
        {
            try
            {
                //判断是否为空
                if (set1name != "" && set2name != "")
                {
                    //数据节点名称为两个数据的名称
                    string dataName = set1name + m_andStr + set2name;
                    string dataNameRe = set2name + m_andStr + set1name;
                    //判断信息是否存在
                    if (m_Xml.ReadValueByKey(dataName, m_keys[0]) != null)
                    {
                        
                        //获取阈值信息,并填充
                        this.Thershold1_Text.Text = m_Xml.ReadValueByKey(dataName, m_keys[0]);
                        this.Thershold2_Text.Text = m_Xml.ReadValueByKey(dataName, m_keys[1]);
                        if (this.Name == "TPPPYZ")
                        {
                            m_comboBox.Text = m_Xml.ReadValueByKey(dataName, m_keys[2]);
                        }
                    }
                    else if (m_Xml.ReadValueByKey(dataNameRe, m_keys[0]) != null)
                    {
                        //获取阈值信息,并填充
                        this.Thershold1_Text.Text = m_Xml.ReadValueByKey(dataNameRe, m_keys[0]);
                        this.Thershold2_Text.Text = m_Xml.ReadValueByKey(dataNameRe, m_keys[1]);
                        if (this.Name == "TPPPYZ")
                        {
                            m_comboBox.Text = m_Xml.ReadValueByKey(dataName, m_keys[2]);
                        }
                    }
                    else
                    {
                        this.Thershold1_Text.Text = null;
                        this.Thershold2_Text.Text = null;
                        if (this.Name == "TPPPYZ")
                        {
                            m_comboBox.SelectedIndex = 0;
                        }
                    }
                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        /// <summary>
        /// 只允许输入数字、一个小数点、删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == (char)8))
                {
                    e.Handled = true;
                }
                else
                {
                    TextBox m_textBox = (TextBox)sender;
                    if (e.KeyChar >= '0' && e.KeyChar <= '9' && m_textBox.Text == "0")
                    {
                        e.Handled = true;
                        m_textBox.Text = (e.KeyChar - 48).ToString();
                    }
                    if (e.KeyChar == '.')
                    {
                        int num = m_textBox.Text.IndexOf('.');
                        if (m_textBox.Text.Length == 0 || num != -1)
                        {
                            e.Handled = true;
                        }
                    }
                    if (e.KeyChar == (char)8 && m_textBox.Text.Length == 1)
                    {
                        e.Handled = true;
                        m_textBox.Text = "0";
                    }
                }
                ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 判断值的范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Thershold1_Text_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.Name == "TPPPYZ")
                {
                    TextBox m_textBox = (TextBox)sender;
                    if (m_textBox.Text != "")
                    {
                        if (double.Parse(m_textBox.Text) >= 1)
                        {
                            m_textBox.Text = "0.000";
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 只允许输入数字,或者删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_KeyPress2(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8))
                {
                    e.Handled = true;
                }
                else
                {
                    string text = ((TextBox)sender).Text;
                    if (e.KeyChar >= '0' && e.KeyChar <= '9' && text == "0")
                    {
                        e.Handled = true;
                        ((TextBox)sender).Text = (e.KeyChar - 48).ToString();
                    }
                    if (e.KeyChar == (char)8 && text.Length == 1)
                    {
                        e.Handled = true;
                        ((TextBox)sender).Text = "0";
                    }
                }
                ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 保存对应的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Thershold1_Text.Text)&& !string.IsNullOrEmpty(Thershold2_Text.Text) && Set1_Combo.SelectedIndex != -1 && Set2_Combo.SelectedIndex != -1)
                {
                    //数据节点名称为两个数据的名称
                    string dataName = Set1_Combo.Text + m_andStr + Set2_Combo.Text;
                    string dataNameRe = Set2_Combo.Text + m_andStr + Set1_Combo.Text;
                    //判断信息是否存在
                    if (m_Xml.ReadChildValues(dataName).Count>0 || m_Xml.ReadChildValues(dataNameRe).Count > 0)
                    {
                        if (MessageBox.Show("当前匹配数据集的阈值信息已存在,是否覆盖已有信息？", "提示:", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            //删除原有数据,属性固定所以只删除一次，就行
                            m_Xml.DeleteXml(m_infoName, dataNameRe);
                            if (this.Name == "KJPPYZ")
                            {
                                //获取用户填入的值
                                string[] m_values = new string[] { Thershold1_Text.Text, Thershold2_Text.Text };
                                //保存距离阈值和长度误差
                                m_Xml.WriteXml(m_infoName, dataName, m_keys, m_values);
                            }
                            else
                            {
                                //获取用户填入的值
                                string[] m_values = new string[] { Thershold1_Text.Text, Thershold2_Text.Text, m_comboBox.Text };
                                //保存距离阈值和长度误差
                                m_Xml.WriteXml(m_infoName, dataName, m_keys, m_values);
                            }
                            //结束
                            MessageBox.Show("保存完成！", "提示：");
                            if (m_datasources == null)
                            {
                                //如果是检查窗口唤起，退出则返回OK
                                this.DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    else
                    {
                        if (this.Name == "KJPPYZ")
                        {
                            //获取用户填入的值
                            string[] m_values = new string[] { Thershold1_Text.Text, Thershold2_Text.Text };
                            //保存距离阈值和长度误差
                            m_Xml.WriteXml(m_infoName, dataName, m_keys, m_values);
                        }
                        else
                        {
                            //获取用户填入的值
                            string[] m_values = new string[] { Thershold1_Text.Text, Thershold2_Text.Text, m_comboBox.Text };
                            //保存距离阈值和长度误差
                            m_Xml.WriteXml(m_infoName, dataName, m_keys, m_values);
                        }
                        //结束
                        MessageBox.Show("保存完成！", "提示：");
                        if (m_datasources == null)
                        {
                            //如果是检查窗口唤起，退出则返回OK
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void No_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //判断窗口在哪里开启的
                if (m_datasources == null)
                {
                    MessageBox.Show("您尚未完成阈值设置，对后续计算可能会有影响。\n您可以在检查完成后，重新打开阈值窗口进行设置", "提示：");
                    //退出
                    this.DialogResult = DialogResult.No;
                }
                else
                {
                    this.Close();
                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 链接点数据集和线数据集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Link_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //实例化链接窗体
                TPPPLJ m_TPPPLJ = new TPPPLJ(m_datasources);
                if(m_TPPPLJ.ShowDialog() != DialogResult.OK)
                {
                    m_TPPPLJ.Dispose();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
